package com.api.services;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import com.api.test.Api;

public class DomainRepository {

	private final Api api;

	private Properties properties;

	private final String propertiesFile;

	private static final String DOMAIN_NAME_PREFIX = "ZyncroTest-";
	private static final String EMAIL_SUFFIX = ".email";
	private static final String APP_ID_SUFFIX = ".appId";
	private static final String PASSWORD_SUFFIX = ".password";

	public DomainRepository(String propertiesFile, Api api) {

		if (propertiesFile == null || api == null) {
			throw new IllegalArgumentException("Constructor fields may not be null");
		}
		this.api = api;
		this.propertiesFile = propertiesFile;
	}

	public synchronized DomainUser getDomainUser(String domainName) {

		if (domainName == null) {
			throw new IllegalArgumentException("DomainName cannot be null");
		}

		this.properties = readPropertiesFile();

		String name = domainName.replaceAll(" ", "_");

		DomainUser user = null;

		if (properties.containsKey(name + EMAIL_SUFFIX)) {

			String email = properties.getProperty(name + EMAIL_SUFFIX);
			String appId = properties.getProperty(name + APP_ID_SUFFIX);
			String password = properties.getProperty(name + PASSWORD_SUFFIX);

			user = new DomainUser(email, password, appId);
		} else {
			user = createDomainUser(domainName);
		}
		return user;
	}

	private Properties readPropertiesFile() {
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(propertiesFile));
		} catch (FileNotFoundException e) {

		} catch (IOException e) {
			throw new IllegalStateException(String.format("Couldn't load properties file %s", propertiesFile), e);
		}
		return props;
	}

	private DomainUser createDomainUser(String domain) {

		String name = domain.replaceAll(" ", "_");

		String domainName = DOMAIN_NAME_PREFIX + name;
		long domainQuota = 1073741824; // 1 GB
		int totalUsers = 2000;
		int domainType = 1;
		String userName = name;
		String lastName = "main_admin";
		String password = "123456";
		String email = name.toLowerCase() + "@yopmail.com";
		long userQuota = 10485760; // 10 MB
		int language = 0;

		Map<String, String> result = api.getService(IZyncroApi.class).createDomainNew(domainName, domainQuota, totalUsers, domainType,
				userName, lastName, password, email, userQuota, language);

		DomainUser user = new DomainUser(email, password, result.get("nameUser"));

		saveNewDomain(name, user);

		return user;
	}

	private void saveNewDomain(String domainName, DomainUser user) {

		try {
			FileWriter fstream = new FileWriter(propertiesFile, true);
			BufferedWriter fbw = new BufferedWriter(fstream);
			fbw.write(domainName + EMAIL_SUFFIX + "=" + user.getEmail());
			fbw.newLine();
			fbw.write(domainName + APP_ID_SUFFIX + "=" + user.getAppId());
			fbw.newLine();
			fbw.write(domainName + PASSWORD_SUFFIX + "=" + user.getPassword());
			fbw.newLine();

			fbw.newLine();

			fbw.close();
		} catch (Exception e) {
			throw new IllegalStateException(String.format("Couldn't save properties in file %s", propertiesFile), e);
		}
	}
}
